import 'package:flutter/material.dart';
import 'package:flutter_public/flutter_public.dart';

import 'model/test_model.dart';
import 'notifier/test_detail_notifier.dart';
import 'notifier/test_list_notifier.dart';

/// FileName test_detail_page
///
/// @Author lax
/// @Date 2024/6/4
///
/// @Description

class TestDetailPage extends StatefulWidget {
  final TestModel? model;

  const TestDetailPage({super.key, this.model});

  @override
  State<StatefulWidget> createState() {
    return _TestDetailPageState();
  }
}

class _TestDetailPageState extends State<TestDetailPage> {
  late TestDetailNotifier _notifier;

  @override
  void initState() {
    super.initState();
    _notifier = TestDetailNotifier(model: widget.model);
  }

  @override
  Widget build(BuildContext context) {
    return HideKeyboardScaffold(
      appBar: PublicAppBar(
          title: widget.model?.id == null
              ? '新增'
              : widget.model?.enable == true
                  ? '编辑'
                  : '详情'),
      body: body(),
      bottomNavigationBar: Visibility(
        visible: _notifier.model.enable,
        child: ShadowContainer(
          padding: SafeEdgeInsets.all(16),
          child: PublicButton(
            height: 40,
            onPressed: () {
              if ((_notifier.model.question ?? '').isEmpty) {
                Toast.show('请输入question');
                return;
              }
              HideKeyboardGesture.tap(context);
              PublicAlertDialog.show(
                  context: context,
                  title: '确认提交吗',
                  onConfirm: () async {
                    final value = await _notifier.submitAction();
                    if (value && context.mounted) {
                      Navigator.pop(context, _notifier.model);
                    }
                  });
            },
            text: '提交',
          ),
        ),
      ),
    );
  }

  Widget body() {
    return Column(
      children: [
        TextSelectItem(
          enable: _notifier.model.enable,
          title: 'corp',
          value: _notifier.model.corpName,
          onTap: () async {
            Loading.show();
            final resp = await Api.get(
                base: TestListNotifier.base,
                url: 'api/base/orgmanager/GetRoleCorp');
            Loading.dismiss();
            final list = resp['Result'] ?? [];
            final dataSource = list
                .map<KeyValueModel>((e) => KeyValueModel(
                      value: e['CropName'] ?? '',
                      key: e['CorpID'].toString(),
                    ))
                .toList();
            if (!context.mounted) {
              return;
            }
            ModalBottomSheet.showSingleSheet(
              context: context,
              title: 'selectCorp',
              dataSource: dataSource,
              selectedId: _notifier.model.corp,
              onConfirm: (value) {
                setState(() {
                  _notifier.model.corp = value?.key;
                  _notifier.model.corpName = value?.value;
                });
              },
            );
          },
        ),
        TextFieldItem(
          enable: _notifier.model.enable,
          title: 'question',
          value: _notifier.model.question,
          onChanged: (text) {
            _notifier.model.question = text;
          },
        )
      ],
    );
  }
}
